<template>
  <el-dialog
    :title="pageType===0?'发布新公告':'编辑公告'"
    :visible.sync="dialogVisible"
    width="50%"
    destroy-on-close
    append-to-body
    custom-class="k-dilaog"
    @closed="closedEvent">
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="公告标题" required>
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="公告内容" required>
        <el-input v-model="form.content" type="textarea" :rows="3"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
     <el-button @click="dialogVisible = false">取 消</el-button>
     <el-button type="primary" @click="clickSureBtn" :loading="loading">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import request from '@/utils/request';

export default {
  name:"noticeIssuedDialog",
  data(){
    return{
      dialogVisible:false,
      loading:false,
      form:{},
      callBack:null,
      pageType:0,//0新增1编辑
      id:null,
    }
  },
  methods:{
    openDiaolog(pageType,id,block){
      this.pageType = pageType;
      this.id = id;
      this.callBack = block;
      this.dialogVisible = true;
      if (pageType === 1){//编辑
        this.requestDataDetail();
      }
    },
    clickSureBtn(){
      if (!this.form.title){
        this.$message.error("请输入标题");
        return
      }
      if (!this.form.content){
        this.$message.error("请输入内容");
        return
      }
      if (this.pageType === 0){//新增
        this.requestDataAdd();
      }else {//编辑
        this.requestDataEdit();
      }
    },
    closedEvent(){
      this.form = {};
    },
    //网络-详情
    requestDataDetail(){
      request({
        url:`/notice_info/${this.id}`,
        method:'get',
        params: {}
      }).then(response =>{
        this.form = response.data;
      })
    },
    //网络-新增
    requestDataAdd(){
      this.loading = true;
      this.form.status = 1;
      request({
        url:`/notice_info`,
        method:'post',
        data: this.form,
      }).then(response =>{
        this.$message.success(response.msg);
        this.callBack();
        this.dialogVisible = false;
      }).finally(()=>{
        this.loading = false;
      })
    },
    //网络-编辑
    requestDataEdit(){
      this.loading = true;
      request({
        url:`/notice_info`,
        method:'put',
        data: this.form,
      }).then(response =>{
        this.$message.success(response.msg);
        this.callBack();
        this.dialogVisible = false;
      }).finally(()=>{
        this.loading = false;
      })
    },
  }
}

</script>
